<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/15
 * Time: 14:27
 */
namespace app\admin\controller;
use think\Db;
class Renovate  extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $params = input('param.');
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['name|address'] = ['like',"%" . $key . "%"];
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $map['sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $map['shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $map['qu'] = $params['qu'];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('company')->where($map)->count();
            $lists = Db::name('company')->alias('u')
                ->join('up_area a1', 'u.sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.qu = a3.district_id', 'left')
                ->where($map)
                ->page($page,$limit)
                ->order('tuijian desc,time desc')
                ->field('u.*,a1.district sheng,a2.district shi,a3.district qu')
                ->select ();
            foreach ($lists as $key=>$vo){
                if($vo['image'] != ''){
                    $photo = explode (',',$vo['image']);
                    $lists[$key]['picture'] = $photo[0];
                    unset($photo[0]);
                    $lists[$key]['image'] = array_values($photo);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }

    public function del(){
        $res = Db::name('company')->where('id',input('id'))->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{

            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    public function tuijianyes(){
        $res = Db::name('company')->where('id',input('id'))->setField('tuijian',2);
        if($res){
            return json(['code'=>200,'msg'=>'成功']);
        }else{
            return json(['code'=>100,'msg'=>'失败']);
        }
    }
    public function tuijianno(){
        $res = Db::name('company')->where('id',input('id'))->setField('tuijian',1);
        if($res){
            return json(['code'=>200,'msg'=>'成功']);
        }else{
            return json(['code'=>100,'msg'=>'失败']);
        }
    }

    public function add(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            if(mb_strlen($name)>20){
                return json(['code'=>100,'msg'=>'装修公司名称不能超过20字']);
            }
            if(mb_strlen($address)>30){
                return json(['code'=>100,'msg'=>'装修公司地址不能超过30字']);
            }
            if(mb_strlen($content)>500){
                return json(['code'=>100,'msg'=>'装修公司服务详情不能超过500字']);
            }
            if(empty($photo)){
                return json(['code'=>100,'msg'=>'图片不能为空']);
            }
            $param = [
                'image'=>$photo,
                'content'=>$content,
                'address'=>$address,
                'sheng'=>$dis_sheng,
                'shi'=>$dis_shi,
                'qu'=>$dis_qu,
                'phone'=>$phone,
                'name'=>$name,
                'title'=>$title,
                'thumb'=>current(explode(',',$photo)),
                'time'=>$time,
                'type'=>"装修公司",
            ];
            $res = Db::name('company')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    public function update(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            if(mb_strlen($name)>20){
                return json(['code'=>100,'msg'=>'装修公司名称不能超过20字']);
            }
            if(mb_strlen($address)>30){
                return json(['code'=>100,'msg'=>'装修公司地址不能超过30字']);
            }
            if(mb_strlen($content)>500){
                return json(['code'=>100,'msg'=>'装修公司服务详情不能超过500字']);
            }
            if(empty($photo)){
                return json(['code'=>100,'msg'=>'图片不能为空']);
            }
            $param = [
                'image'=>$photo,
                'content'=>$content,
                'address'=>$address,
                'sheng'=>$dis_sheng,
                'shi'=>$dis_shi,
                'qu'=>$dis_qu,
                'phone'=>$phone,
                'name'=>$name,
                'title'=>$title,
                'thumb'=>current(explode(',',$photo)),
                'time'=>$time,
                'type'=>"装修公司",
            ];
            $res = Db::name('company')->where ('id',$id)->update ($param);
            if($res){
                return json(['code'=>200,'msg'=>'成功']);
            }else{
                return json(['code'=>100,'msg'=>'失败']);
            }
        }
        $id = input('id');
        $dis = Db::name('company')->where ('id',$id)->find();
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        // 市
        $city = Db::table('up_area')->where('level', 2)->field('district_id, district')->select();
        // 区
        $district = Db::table('up_area')->where('level', 3)->field('district_id, district')->select();
        $this->assign([
            'id' => $id,
            'dis' => $dis,
            'province' => $province,
            'city' => $city,
            'district' => $district
        ]);
        return $this->fetch();
    }
}